﻿using UnityEngine;
using System.Collections;

namespace play {
	public class TurnArond : MonoBehaviour {
		Ctrl ctrl;

		public bool Faceright = false;
		public bool Faceleft = false;
		public bool Facebehind = false;
		public bool FaceAhead = true;
	
		//public float maxSpeed = 5f;
		//public float Force = 0.0005f;

	//	Vector3 pos;
		// Use this for initialization
		void Start () {
			ctrl = GetComponent<Ctrl> ();
		}

		// Update is called once per frame
		void Update () {
			FaceAhead = ctrl.state.direction ==Common.UP?true:false;
			Faceleft  = ctrl.state.direction ==Common.LEFT?true:false;
			Faceright = ctrl.state.direction ==Common.RIGHT?true:false;
			Facebehind =ctrl.state.direction ==Common.DOWN?true:false;
		//	pos = transform.position;
		//	if(PlayerState.hasInertia){
		//		autoRun ();
		//	}
		}

		private void startTurn(float hX,float hY){
			//前转左
			if(hX<0&&FaceAhead){
				turn(false,true,false,false,90);
			}
			//前转右
			else if(hX>0&&FaceAhead){
				turn(false,false,true,false,-90);
			}
			//前转后
			else if(hY<0&&FaceAhead){
				turn(false,false,false,true,180);
			}
			//左转前
			else if(hY>0&&Faceleft){
				turn(true,false,false,false,-90);
			}
			//左转后
			else if(hY<0&&Faceleft){
				turn(false,false,false,true,90);
			}
			//左转右
			else if(hX>0&&Faceleft){
				turn (false,false,true,false,180);
			}
			//后转左
			else if(hX<0&& Facebehind){
				turn (false,true,false,false,-90);
			}
			//后转右
			else if(hX>0&&Facebehind){
				turn (false,false,true,false,90);
			}
			//后转前
			else if(hY>0&&Facebehind){
				turn(true,false,false,false,180);
			}
			//右转前
			else if(hY>0&&Faceright){
				turn (true,false,false,false,90);
			}
			//右转左
			else if(hX<0&&Faceright){
				turn (false,true,false,false,180);
			}
			//右转后
			else if(hY<0&&Faceright){
				turn (false,false,false,true,-90);
			}

		}
		//turn the tank's angle
		private void turn(bool ahead,bool left,bool right,bool behind,int angel){
			FaceAhead = ahead;
			Faceleft = left;
			Faceright  =right;
			Facebehind = behind;

			if(ahead){
				ctrl.state.direction = Common.UP;
			}

			if(ahead){
				ctrl.state.direction = Common.UP;
			}
			
			if(left){
				ctrl.state.direction = Common.LEFT;
			}
			
			if(right){
				ctrl.state.direction = Common.RIGHT;
			}
			
			if(behind){
				ctrl.state.direction = Common.DOWN;
			}

			transform.Rotate(Vector3.forward*angel);

		}

		/*	void OnEnable() {
			EasyJoystick.On_JoystickMove += OnJoystickMove;
			EasyJoystick.On_JoystickMoveEnd += OnJoystickMoveEnd;
			
		}  
		//移动摇杆结束  
		
		void OnJoystickMoveEnd(MovingJoystick move) {
		}
		//移动摇杆中  
		
		void OnJoystickMove(MovingJoystick move) {
			//死了就不能乱动了
			if (move.joystickName != "MoveJoystick" || !ctrl.state.isAlive) {
				return;
			}
			//惯性让我漂移
			if(ctrl.state.hasInertia){
				return;
			}

			//获取摇杆中心偏移的坐标
			float hX = move.joystickAxis.x;
			float hY = move.joystickAxis.y;

			hX = Mathf.Abs (hX)<Mathf.Abs (hY)?0:Mathf.Sign(hX);
			hY = Mathf.Abs (hX)>Mathf.Abs (hY)?0:Mathf.Sign(hY);
			Vector3 targetPosition = transform.position;

			if(hY==0){
				//转向
				startTurn(hX,0.0f);
				//小针检测+移动
				//右移
				if(hX>0){
					// 向右移动
					if (ctrl.canMove(Common.RIGHT)) {
						//transform.position = new Vector3(targetPosition.x - Time.deltaTime * Speed, targetPosition.y, targetPosition.z);
						targetPosition = new Vector3(transform.position.x+hX*ctrl.state.speed*Time.deltaTime,transform.position.y,0.0f);
					}

				}
				//左移
				else if(hX<0){
					// 向左移动
					if (ctrl.canMove(Common.LEFT)) {
						//transform.position = new Vector3(targetPosition.x - Time.deltaTime * Speed, targetPosition.y, targetPosition.z);
						targetPosition = new Vector3(transform.position.x+hX*ctrl.state.speed*Time.deltaTime,transform.position.y,0.0f);
					}
				}
			}
			else if(hX==0){
				//转向
				startTurn(0.0f,hY);
				//小针检测+移动
				//上移
				if(hY>0){
					// 向上移动
					if (ctrl.canMove(Common.UP)) {
						//transform.position = new Vector3(targetPosition.x - Time.deltaTime * Speed, targetPosition.y, targetPosition.z);
						targetPosition = new Vector3(transform.position.x,transform.position.y+hY*ctrl.state.speed*Time.deltaTime,0.0f);
					}

				}
				//下移
				else if(hY<0){
					// 向下移动
					if (ctrl.canMove(Common.DOWN)) {
						//transform.position = new Vector3(targetPosition.x - Time.deltaTime * Speed, targetPosition.y, targetPosition.z);
						targetPosition = new Vector3(transform.position.x,transform.position.y+hY*ctrl.state.speed*Time.deltaTime,0.0f);
					}
				}
			}
			
			transform.position = targetPosition;
		}

	}*/
	}
}